<?php
class User_bank_model extends BZ_Model
{
    protected $_table  = 'user_bank';
    protected $_table1 = 'pay_method';

    //列表
    public function findByUid($uid)
    {
        $this->db->select('user_bank_id, bank_name, bank_card, bank_id, bank_type, mobile_phone, created_at, cardholder, is_default');
        $this->db->from($this->_table);
        $this->db->where('uid', $uid);
        $this->db->order_by('is_default', 'DESC');
        return $this->db->get();
    }

    public function findById($id, $uid)
    {
        $this->db->select('user_bank_id, bank_name, bank_card, bank_id, bank_type, mobile_phone, created_at, cardholder, is_default');
        $this->db->from($this->_table);
        $this->db->where('uid', $uid);
        $this->db->where('user_bank_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    public function findBankInfo($id, $uid)
    {
        $this->db->select('user_bank_id, user_bank.bank_name, bank_card, bank_type, mobile_phone, created_at, cardholder, is_default, bank_img');
        $this->db->from($this->_table);
        $this->db->join($this->_table1, 'pay_method.bank_id = user_bank.bank_id', 'INNER');
        $this->db->where('uid', $uid);
        $this->db->where('user_bank_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function updateInfo($id, $uid, $dataInfo)
    {
        $this->db->where('user_bank_id', $id);
        $this->db->where('uid', $uid);
        return $this->db->update($this->_table, $dataInfo);
    }
    
    //删除银行卡
    public function delete($id, $uid)
    {
        $this->db->where('uid', $uid);
        $this->db->where('user_bank_id', $id);
        return $this->db->delete($this->_table);
    }

    /**
     * 设置为非默认
     */
    public function setNoDefault($uid)
    {
        $this->db->set('is_default', 0, FALSE);
        $this->db->where('uid', $uid);
        return $this->db->update($this->_table);
    }

    public function total($uid)
    {
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where('uid', $uid);
        return $this->db->count_all_results();
    }
}